class ActiveUser < ActiveRecord::Base
   belongs_to :user
   belongs_to :last_topic, :class_name=>"Topic", :foreign_key => "last_topic_id"
   belongs_to :last_company, :class_name=>"Company", :foreign_key => "last_company_id"
   belongs_to :last_incumbent, :class_name=>"Incumbent", :foreign_key => "last_incumbent_id"
   belongs_to :last_report, :class_name=>"Report", :foreign_key => "last_report_id"
  
  #Active ActiveUser Portal Controller
  
  def create_user(params_for_user,params_for_profile)
    new_user = User.new(params_for_user)
    new_user.created_ip=self.active_user_ip
    new_user.profile = Profile.new(params_for_profile)
    return new_user
  end
  
  def create_company(params_for_company)
    return user.banned? ? nil : user.create_company(params_for_company)
  end
  
  def create_incumbent(params,company)
    return user.banned? ? nil : user.create_incumbent(params,company)
  end
  
  def create_topic(params,forum)
    return user.banned? ? nil : user.create_topic(params,forum)
  end
  
  def create_picture(params,cat_id,obj_id)
    return user.create_picture(params,cat_id,obj_id)
  end
  
  def create_outbox_message(mgser_id)
    return self.guest? ? nil : user.create_outbox_message(mgser_id)
  end
  
  def create_friend(friend_id)
    return self.guest? ? nil : user.create_friend(friend_id)
  end
  
  def delete_picture(id)
    return user.delete_picture(id)
  end
  
  def update_topic(params,topic)
    return user.update_topic(params,topic)
  end
  
  def create_comment(params,obj)
    return user.banned? ? nil : user.create_comment(params,obj,self.active_user_ip)
  end
  
  def update_comment(params,comment)
    return user.update_comment(params,comment)
  end
  
  def update_password(old_pw,new_pw,confirm_pw)
    return self.guest? ? "对不起，你无权修改密码" : user.update_password(old_pw,new_pw,confirm_pw)
  end
  
  def update_tag(obj,search_tag)
    return self.guest? ? "对不起，你无权更新标签" : user.update_tag(obj,search_tag)
  end
  
  def update_user(params)
    return self.guest? ? "对不起，你无权更改资料" : user.update_user(params)
  end
  
  def update_profile(params)
    return self.guest? ? "对不起，你无权更改资料" : user.update_profile(params)
  end
  
  def delete_incumbent(incumbent_id)
    return self.guest? ? false : user.delete_incumbent(incumbent_id)
  end
  
  def delete_company(company_id)
    return self.guest? ? false : user.delete_company(company_id)
  end
  
  def guest?
    return user_id == GUEST_USER_ID
  end
  
  def own(obj)
    return user.own(obj)
  end
  
  def is_friend(user)
    return self.guest? ? false : user.is_friend(user)
  end
  
  def adminstrative?
    return user.adminstrative?
  end
  
  def outbox_messages
    return self.guest? ? nil : user.outbox_messages
  end
  
  def inbox_messages
    return self.guest? ? nil : user.inbox_messages
  end
  
  def try_to_login(params)
    user = User.login(params[:name], params[:password])
    unless user.nil?
      params[:remember_me] == "1" ? user.remember_me : user.forget_me
      user.after_login_success(self)
      return true
    else
      return false
    end
  end
  
  def create_message(mgser_id,params)
    return self.guest? ? "非注册用户，无法发送邮件" : user.create_message(mgser_id,params)
  end
  
  def create_query_conditions(query,category_id,additional_conditions="")
    User.find_by_id(self.user_id).create_query_conditions(query,category_id,additional_conditions)
  end
  
  def validate_active_user(user_ip)
    unless self.active_user_ip==user_ip
      self.user_id=GUEST_USER_ID
      self.active_user_ip=user_ip
    end
    self.last_active_at=Time.now
    self.save
  end
  
  def login_from_cookie(auth_token)
    self.user = User.find_by_remember_token(auth_token) if self.guest? && auth_token
    self.user_id = GUEST_USER_ID unless self.user
  end
  
  def log_action(control_name,action_name,obj_id)
    self.last_obj_id=obj_id
    self.last_control=control_name
    self.last_action=action_name
    self.user.log_action(control_name,action_name,obj_id)
    self.save
  end
  
  def is_friend(checker)
    return self.user.is_friend(checker)
  end
  
  
  def update_correlation(obj)
      thecategory=Category.find_category(obj)
      if thecategory.name=="Topic"
        Correlation.update_correlation(obj,self.last_topic)
        self.last_topic=obj
      elsif thecategory.name=="Company"
        Correlation.update_correlation(obj,self.last_company)
        self.last_company=obj
      elsif thecategory.name=="Incumbent"
        Correlation.update_correlation(obj,self.last_incumbent)
        self.last_incumbent=obj
      elsif thecategory.name=="Report"
        Correlation.update_correlation(obj,self.last_report)
        self.last_report=obj
      end
      self.save
  end
  
  def self.active_user(active_user_id,user_ip,auth_token)
    if activeuser=ActiveUser.find_by_id(active_user_id)
      activeuser.login_from_cookie(auth_token)
      activeuser.validate_active_user(user_ip)
    else
      activeuser=ActiveUser.new
      activeuser.active_user_ip=user_ip
      activeuser.last_active_at=Time.now
      activeuser.user_id = GUEST_USER_ID
      activeuser.login_from_cookie(auth_token)
      activeuser.save
    end
    return activeuser
  end
  
  def self.remove_active_user(active_user_id)
    if activeuser=ActiveUser.find_by_id(active_user_id)
      User.find_by_id(activeuser.user_id).logout unless activeuser.guest?
      activeuser.destroy
    end
  end
end
